package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/checkgroup")
@RestController
public class CheckgroupController {

    @Reference
    CheckGroupService service;

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    public Result add(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup){
        try {
            service.add(checkitemIds,checkGroup);
            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
        }  catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean q){
            PageResult pageResult = service.findPage(q);
        return pageResult;
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findById(Integer id){
        try {
            CheckGroup checkGroup =  service.findById(id);
            return  new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findCheckItemIdsByCheckGroupId(Integer id){

        try {
            Integer [] CheckItemIds =  service.findCheckItemIdsByCheckGroupId(id);
            return  new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,CheckItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result edit (Integer[] checkitemIds, @RequestBody CheckGroup checkGroup ){
        try {
            service.edit(checkitemIds,checkGroup);
            return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/delById")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result delById(Integer id){
        try {
            service.delById(id);
            return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findAll(){

        try {
            List<CheckGroup> checkGroupList =  service.findAll();
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }


}
